//
//  LoanInformationViewController.swift
//  IDF
//
//  Created by 陈徐挺 on 26/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit
import SVProgressHUD

class LoanInformationViewController: BaseViewController {

    @IBOutlet weak var totalPercentLabel: UILabel!
    @IBOutlet weak var pPercentLabel: UILabel!
    @IBOutlet weak var wPercentLabel: UILabel!
    @IBOutlet weak var cPercentLabel: UILabel!
    @IBOutlet weak var uPercentLabel: UILabel!
    @IBOutlet weak var oPercentLabel: UILabel!
    @IBOutlet weak var progressWidth: NSLayoutConstraint!
    @IBOutlet weak var nextBtn: UIButton!

    var loanInformation: LoanInformation!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Loan information"

        nextBtn.isEnabled = false
        nextBtn.alpha = 0.4
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func getData() {
        //SVProgressHUD.show()
        let _ = Selection.getList().then { selectionArr -> Void in
            appSelection = selectionArr
        }

        let _ = appUser.getLoanInformation().then { [weak self] loanInformation -> Void in
            self?.loanInformation = loanInformation
            self?.progressWidth.constant = CGFloat(kWidth - 20 - 20) * CGFloat(loanInformation.allPercent!) / 100
            self?.totalPercentLabel.text = "Data integrity : \(loanInformation.allPercent!)%"
            self?.pPercentLabel.text = "\(loanInformation.personalPercent!)%"
            self?.wPercentLabel.text = "\(loanInformation.workingPercent!)%"
            self?.cPercentLabel.text = "\(loanInformation.contactPercent!)%"
            self?.uPercentLabel.text = "\(loanInformation.cardPercent!)%"
            self?.oPercentLabel.text = "\(loanInformation.otherInfoPercent!)%"

            if loanInformation.personalPercent == 100 {
                self?.nextBtn.isEnabled = true
                self?.nextBtn.alpha = 1
            } else {
                self?.nextBtn.isEnabled = false
                self?.nextBtn.alpha = 0.4
            }

        }
    }

    @IBAction func moreInfoClick(_ sender: UIButton) {
        
        guard let _ = loanInformation else {
            return 
        }
        
        switch sender.tag {
        case 0:
            let next: PersonalInformationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "personalInformationViewController")
            next.userPersonalInfo = loanInformation.userPersonalInfo
            nowNavgationController.pushViewController(next, animated: true)
        case 1:
            let next: WorkingInformationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "workingInformationViewController")
            next.userWorkingInfo = loanInformation.userWorkingInfo
            nowNavgationController.pushViewController(next, animated: true)
        case 2:
            let next: ContactInformationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "contactInformationViewController")
            next.userContactInfoArr = loanInformation.userContactInfo
            nowNavgationController.pushViewController(next, animated: true)
        case 3:
            let next: UploadPhotoViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "uploadPhotoViewController")
            next.userCard = loanInformation.userCards
            nowNavgationController.pushViewController(next, animated: true)
        case 4:
            let next: OtherInformationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "otherInformationViewController")
            next.otherInfo = loanInformation.otherInfo
//            next.userCard = loanInformation.userCards
            nowNavgationController.pushViewController(next, animated: true)
        case 5:
            let _ = appUser.getBankInfo().then { obj -> Void in
                let next: InformationConfirmationViewController = initViewContollerWithStoryboard(storyboardName: "Home", storyboardId: "informationConfirmationViewController")
                next.bankInfo = obj
                nowNavgationController.pushViewController(next, animated: true)
            }
        default:
            break
        }

    }




    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
